import 'package:flutter/material.dart';
import 'package:flutter_animation/Utils/ColorUtil.dart';
import 'package:flutter_animation/Utils/SPUtil.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import 'Home/MyHomePage.dart';
import 'Login/Login.dart';

//原生的启动屏幕
import 'package:flutter_native_splash/flutter_native_splash.dart';

import 'Utils/DB/MessageDao.dart';
import 'Utils/STTUtil.dart';

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  initialization(null);
  late StatefulWidget materialApp;
  await SPUtil.instance.init();
  await MessageDao.instance.init();
  try {
    await STTUtil.init();
  } catch (e) {
    print(e);
  }
  String email = SPUtil.get(SPUtil.email);
  if (email.isEmpty) {
    materialApp = const Login();
  } else {
    materialApp = const MyHomePage();
  }
  runApp(MyApp(materialApp: materialApp));
}

class MyApp extends StatelessWidget {
  final StatefulWidget materialApp;
  const MyApp({super.key, required this.materialApp});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 667),
      minTextAdapt: true,
      splitScreenMode: true,
      // Use builder only if you need to use library outside ScreenUtilInit context
      builder: (_, child) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            textSelectionTheme: const TextSelectionThemeData(
              cursorColor: ColorUtil.color999999, // 游标颜色
              selectionColor: ColorUtil.color999999, // 设置选中文字的背景颜色
              selectionHandleColor: ColorUtil.color999999, // 设置选中文字的手柄颜色
            ),
          ),
          debugShowCheckedModeBanner: false,
          locale: const Locale('zh', 'CN'),
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: const [
            Locale('zh', 'CN'),
            Locale('en', 'US'),
          ],
          builder: FlutterSmartDialog.init(),
          home: materialApp,
          // builder: (context, widget) {
          //   return MediaQuery(
          //     //设置文字大小不随系统设置改变
          //     data: MediaQuery.of(context).copyWith(textScaler: const TextScaler.linear(1.0)),
          //     child: !,
          //   );
          // },
        );
      },
    );
  }
}

//启动图延时移除方法
void initialization(BuildContext? context) async {
  //延迟3秒
  await Future.delayed(const Duration(seconds: 3));
  FlutterNativeSplash.remove();
}